/**
 * Created by sa on 2015/4/15.
 */

var moment = require('moment');

/**
 * 时间转化
 * @param dateStr
 * @returns {Date}
 */
function str2Date(date) {

  if (isValidDateTime(date)) {
    return new Date(Date.parse(date.replace(/-/g, "/")));
  }
  return null;
}

/**
 *时间格式化
 * @param time
 * @param format yyyy-MM-dd HH:mm:ss
 * @returns {XML|string|void}
 */
function format1(time) {
  var t = new Date(time);
  return t.format("yyyy-MM-dd HH:mm:ss");
}

/**
 * 时间格式转换
 * @param time
 * @returns {*}
 */
function format4Moment(time){
  return moment(time).format('YYYY-MM-DD HH:mm:ss');
}

/**
 *时间格式化
 * @param time
 * @param format yyyy-MM-dd HH:mm:ss
 * @returns {XML|string|void}
 */
function format(time, format) {
  var t = new Date(time);
  var tf = function (i) {
    return (i < 10 ? '0' : '') + i
  };
  format = format || 'yyyy-MM-dd HH:mm:ss';
  return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
    switch (a) {
      case 'yyyy':
        return tf(t.getFullYear());
        break;
      case 'MM':
        return tf(t.getMonth() + 1);
        break;
      case 'mm':
        return tf(t.getMinutes());
        break;
      case 'dd':
        return tf(t.getDate());
        break;
      case 'HH':
        return tf(t.getHours());
        break;
      case 'ss':
        return tf(t.getSeconds());
        break;
    }
  })
}

/**
 * 验证时间格式
 * 2015-04-10 08:09:05
 * 2015/04/10 08:09:05
 * @returns {boolean}
 */
function isValidDateTime(val) {
  var datePattern = /^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s((([0-1][0-9])|(2?[0-3]))\:([0-5]?[0-9])((\s)|(\:([0-5]?[0-9])))))?$/;
  return datePattern.test(val);
}

/**
 * 验证日期格式
 * 2015-04-10
 * 2015/04/10
 * @returns {boolean}
 */
function isValidDate(val) {
  //var datePattern = /^\d{4}-\d{1,2}-\d{1,2}$/;
  var datePattern = /^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s((([0-1][0-9])|(2?[0-3]))\:([0-5]?[0-9])((\s)|(\:([0-5]?[0-9])))))?$/;
  return datePattern.test(val);
}

/**
 * 验证时间格式
 * 08:09:05
 * 08:09:05
 * @returns {boolean}
 */
function isValidTime(val) {
  var datePattern = /^(?:(?:[0-2][0-3])|(?:[0-1][0-9])):[0-5][0-9]:[0-5][0-9]$/;
  return datePattern.test(val);
}

/**
 * 判断时间是不是在今天
 * @param data
 * @returns {boolean}
 */
function inToday(data){

  if(data == null){
    return false;
  }
  var lsatTimeStr = moment(new Date()).format('YYYY-MM-DD 23:59:59');
  var lastTime = new Date(lsatTimeStr).getTime();
  var now =  new Date().getTime();
  return now < data.getTime() && lastTime >= data.getTime();
}

//console.log(inToday(new Date("2015-04-16 03:59:59")))

function DateNow() {
  return (new Date(Date.now())).format('yyyy/MM/dd hh:mm:ss');
}

function MonthNow () {
  return (new Date(Date.now())).format('yyyyMM');
}

function TodayNow() {
  return (new Date(Date.now())).format('yyMMdd');
}

function Today() {
  return (new Date()).format('yyyy-MM-dd');
}

module.exports.str2Date = str2Date;
module.exports.format = format;
module.exports.format1 = format1;
module.exports.isValidDateTime = isValidDateTime;
module.exports.isValidDate = isValidDate;
module.exports.isValidTime = isValidTime;
module.exports.Today = Today;
module.exports.TodayNow = TodayNow;
module.exports.MonthNow = MonthNow;
module.exports.DateNow = DateNow;
module.exports.inToday = inToday;
module.exports.format4Moment = format4Moment;

